package com.huifaapp.mall.selectImage;

import android.app.Activity;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.huifaapp.mall.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;

import java.util.List;

/**
 * Created by Administrator on 2017/6/21.
 */

public class ImageAdapter extends BaseAdapter {
    Activity baseActivity;
    List<ImagePath> lists;
    ImageLoader imageLoader;
    DisplayImageOptions options;

    public DisplayImageOptions imageOption() {
        return imageOption(R.drawable.icon_empty);
    }

    public DisplayImageOptions imageOption(int emptyId) {
        DisplayImageOptions.Builder builder = new DisplayImageOptions.Builder();
        builder.showImageForEmptyUri(emptyId);
        builder.showImageOnFail(emptyId);
        builder.showImageOnLoading(emptyId);
        builder.cacheOnDisk(true);
        builder.cacheInMemory(true);
        builder.bitmapConfig(Bitmap.Config.ALPHA_8);
        builder.imageScaleType(ImageScaleType.EXACTLY);
        builder.considerExifParams(true);
        return builder.build();
    }

    public ImageAdapter(Activity baseActivity, List<ImagePath> lists){
        this.baseActivity = baseActivity;
        this.lists = lists;
        imageLoader = ImageLoader.getInstance();
        options = imageOption();
    }

    @Override
    public int getCount() {
        return lists.size();
    }

    @Override
    public ImagePath getItem(int i) {
        return lists.get(i);
    }

    @Override
    public long getItemId(int i) {
        return lists.get(i).getId();
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ImagePath imagePath = lists.get(i);
        view = LayoutInflater.from(baseActivity).inflate(R.layout.item_add_image, null);
        ImageView imageView = (ImageView) view.findViewById(R.id.imageView);
        View addBtnLayout = view.findViewById(R.id.addBtnLayout);
        if (imagePath.getId() == -1) {
            //添加
            addBtnLayout.setVisibility(View.VISIBLE);
            imageView.setVisibility(View.GONE);
        } else {
            addBtnLayout.setVisibility(View.GONE);
            imageView.setVisibility(View.VISIBLE);
            if (imagePath.getIsHttp() == 0) {
                // 本地图片
                imageLoader.displayImage("file://" + imagePath.getUrl(), imageView, options);
            } else {
                // http图片
                imageLoader.displayImage(imagePath.getUrl(), imageView, options);
            }
        }
        return view;
    }
}
